const Controller = require("uni-cloud-router").Controller;
const {validateToken} = require('validdate-token');
module.exports = class ProjectAdminController extends Controller{
	/**
	 * @param {Object} token
	 * 验证token
	 */
	async _checkToken(token){
		let tokenRes;
		try{
			tokenRes = await validateToken(token);
			if(tokenRes.code !== 0){
				return{
					status:-1,
					msg:"稍后重试",
					errCode:tokenRes.errCode
				}
			} else {
				return {
					status:0,
					msg:"token有效",
				}
			}
		}catch(e){
			return {
			  status: -1,
			  errCode: 'TOKEN_INVALID',
			  msg: 'token无效'
			}
		}
	}
	/**
	 * 2020-08-15
	 * 通过 “项目状态” 和 “项目经理ID” 获取项目信息
	 */
	async getProjectInfo(){
		let {userID,statusID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getProjectInfo({userID,statusID});
		}
	}
	/**
	 * 2020-08-15
	 * 保存项目信息
	 */
	async setProjectInfo(){
		return this.service.projectAdmin.setProjectInfo();
	}
	/**
	 * 2020-08-18
	 * 通过项目ID获取项目详细信息
	 */
	async getProjectDetail(){
		let {id} = this.ctx.data;
		return this.service.projectAdmin.getProjectDetail({id});
	}
	/**
	 * 2020-11-22
	 * 修改项目结算方式 | 工人结算方式
	 * type | 修改项目 还是员工 （project | worker）
	 * token 
	 * id | 项目ID 或者 员工ID
	 * salary_way | 结算方式
	 */
	async updateProjectSalaryWay(){
		let {type,token,id,salary_way} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.updateProjectSalaryWay({type,id,salary_way});
		}
	}
	/**
	 * 更改项目状态
	 * 2020-09-02
	 */
	async updateProjectStatus(){
		let {token,id,proStatus,projectDate} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.updateProjectStatus({id,proStatus,projectDate});
		}
	}
	/**
	 * 修改工资结算标准
	 */
	async updateSalaryStandard(){
		let {form,token,projectID} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.updateSalaryStandard({form,projectID});
		}
	}
	/**
	 * 更改钻机状态 作业改为完成
	 * 2020-12-26
	 */
	async updateHoleStatus(){
		let {projectID,token,hole,status} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.updateHoleStatus({projectID,hole,status});
		}
	}
	/**
	 * 获取项目员工ID
	 * 2020-11-19
	 */
	async getProjectWKID(){
		let {projectID,token} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getProjectWKID({projectID});
		}
	}
	/**
	 * 2020-11-21
	 * 获取员工信息
	 */
	async getWorkerInfo(){
		let {token,wkID} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getWorkerInfo({wkID});
		}
	}
	/**
	 * 2020-11-21
	 * 获取项目支出信息
	 */
	async getProjectExpend(){
		let {token,projectID,pageNo,pageSize} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getProjectExpend({projectID,pageNo,pageSize});
		}
	}
	/**
	 * 2020-11-21
	 * 获取项目收入信息
	 */
	async getProjectIncome(){
		let {token,projectID,pageNo,pageSize} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getProjectIncome({projectID,pageNo,pageSize});
		}
	}
	/**
	 * 2020-11-24
	 * 员工根据项目ID获取 项目工作信息
	 */
	async wkGetWorkerInfo(){
		let {token,projectID,wkID,pageNo,pageSize} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.wkGetWorkerInfo({projectID,wkID,pageNo,pageSize});
		}
	}
	/**
	 * 2020-12-27
	 * 获取员工被当前项目踢出的信息
	 */
	async getWKhistoryProject(){
		let {token,id,type} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.getWKhistoryProject({id,type});
		}
	}
	/**
	 * 2020-11-21
	 * 上传收入
	 */
	async uploadIncome(){
		let {token,data,type} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.uploadIncome({data,type});
		}
	}
	/**
	 * 2020-11-21
	 * 上传支出
	 */
	async uploadExpend(){
		let {token,data} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.uploadExpend({data});
		}
	}
	/**
	 * 2021-3-28
	 * 删除项目
	 */
	async deleteProject(){
		let {token,id} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.deleteProject(id);
		}
	}
	/**
	 * 2021-3-28
	 * 编辑项目
	 */
	async editProject(){
		let {token,data,_id} = this.ctx.data;
		let checkTokenRes = this._checkToken(token);
		if(checkTokenRes.status === -1){
			return checkTokenRes;
		} else {
			return this.service.projectAdmin.editProject(data,_id);
		}
	}
}